package com.demo.zhou.sniper.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.demo.zhou.sniper.R;
import com.demo.zhou.sniper.application.MyApplication;
import com.demo.zhou.sniper.dbmanger.SPManager;
import com.demo.zhou.sniper.gen.DaoSession;
import com.demo.zhou.sniper.gen.QunbeanDao;
import com.demo.zhou.sniper.module.Qunbean;
import com.demo.zhou.sniper.module.Yxintiaojian;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhou on 2017/11/14.
 */

public class SetDetailsActivity extends BaseActivity implements View.OnClickListener {
    private TextView setd_tv_details;//设置条件详情
    private Spinner setd_sp_qun;//选择应用值
    private Button setd_bt_return, setd_bt_modify;
    private EditText setd_et_colort;//选择需要运行的色表数。
    private Long qun_id;
    private String qunname;
    final List<QunData> qun_data = new ArrayList<>();
    DaoSession daoSession;
    QunbeanDao qunbeanDao;

    String primitives;
    String mode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("详情");
        setRightBtnVisible(false);
        Intent i = getIntent();
        primitives = i.getStringExtra("primitives");
        mode = i.getStringExtra("mode");
        intoview();
        intoData();
    }

    private void intoview() {
        setd_tv_details =  findViewById(R.id.setd_tv_details);
        setd_bt_return =  findViewById(R.id.setd_bt_return);
        setd_bt_modify =  findViewById(R.id.setd_bt_modify);
        setd_sp_qun =  findViewById(R.id.setd_sp_qun);
        setd_et_colort = findViewById(R.id.setd_et_colort);
        setd_bt_return.setOnClickListener(this);
        setd_bt_modify.setOnClickListener(this);
        //从数据库中获取到群的数据
        if (queryQun() == null) {
            Toast.makeText(this, "未建群！", Toast.LENGTH_SHORT).show();
            setd_bt_modify.setEnabled(false);
        } else {
            qun_data.addAll(queryQun());
            qun_id = qun_data.get(0).qunId;
            qunname = qun_data.get(0).qunName;
        }
        List<String> qunDatas = new ArrayList<>();
        for (QunData qunData : qun_data) {
            qunDatas.add(qunData.qunName);
        }
        setd_sp_qun.setAdapter(setAdapter(qunDatas));
        setd_sp_qun.setSelection(0, true);
        setd_sp_qun.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //群数据处理
                qun_id = qun_data.get(position).qunId;
                qunname = qun_data.get(position).qunName;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_setdetails;
    }

    private void intoData() {
        //初始化数据
        Yxintiaojian yxintiaojian = new Yxintiaojian();
        yxintiaojian.setTongcmax(SPManager.getInstance().getInt("tongcmax", 0));
        yxintiaojian.setTongcmix(SPManager.getInstance().getInt("tongcmix", 0));
        yxintiaojian.setJshu(SPManager.getInstance().getFloat("jshu", 0));
        yxintiaojian.setGongzxian(SPManager.getInstance().getInt("gongzxian", 0));
        yxintiaojian.setFanc(SPManager.getInstance().getFloat("fanc", 0));
        yxintiaojian.setWangfanjd(SPManager.getInstance().getFloat("wangfanjd", 0));
        yxintiaojian.setDzhuangtai(SPManager.getInstance().getString("dzhuangtai", ""));
        yxintiaojian.setYinyongz(SPManager.getInstance().getString("yinyongz", ""));

        yxintiaojian.setZtaixz(SPManager.getInstance().getBoolean("ztaixz", false));
        String str_yxtj = "基  数：" + yxintiaojian.getJshu() + "\n工作线：" + yxintiaojian.getGongzxian()
                + "\n返程值：" + yxintiaojian.getFanc() + "\n往返节点：" + yxintiaojian.getWangfanjd()
                + "\n应用值：" + yxintiaojian.getYinyongz() +  "\n运行状态：" + (yxintiaojian.isZtaixz() ? "组状态" : "线状态");
        setd_tv_details.setText(str_yxtj);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setd_bt_return:
                Intent intent = new Intent(SetDetailsActivity.this, SetActivity.class);
                if (primitives != null) {
                    intent.putExtra("primitives", primitives);
                    intent.putExtra("mode", mode);
                    startActivity(intent);
                    finish();
                } else {
                    startActivity(intent);
                    finish();
                }

                break;
            case R.id.setd_bt_modify:
                if (!setd_et_colort.getText().toString().equals("")){
                    Intent intent1 = new Intent(SetDetailsActivity.this,
                            HomeActivity.class);
                    if (SPManager.getInstance().getInt("Jurisdiction", 0)>3){
                        //权限大于三时。。。
                        if (getQunDate()!=null){
                            intent1.putExtra("qunid", getQunDate().qunId+"");
                            intent1.putExtra("qunname", getQunDate().qunName+"");
                        }else {
                            int i = 0;
                        }
                    }else {
                        intent1.putExtra("qunid", qun_id + "");
                        intent1.putExtra("qunname", qunname + "");
                    }
                    int colort = Integer.parseInt(setd_et_colort.getText().toString());
                    intent1.putExtra("colortnum",colort);
                    if (primitives != null) {
                        intent1.putExtra("primitives", primitives);
                        intent1.putExtra("mode", mode);
                    }
                    startActivity(intent1);
                    finish();
                }else {
                    Toast.makeText(this,"请填写需要运行的色表数！",Toast.LENGTH_SHORT).show();
                }

                break;
            default:
                break;

        }

    }
    //获取前两位是aq的群
    protected QunData getQunDate(){
        for(QunData qunData:qun_data){
            String str_data = qunData.qunName;
            if (str_data.substring(0,2).equals("AQ")){
                return  qunData;
            }
        }
        return null;
    }

    public List<QunData> queryQun() {
        //--执行查询操作
        List<QunData> qunDatas = new ArrayList<>();
        daoSession = ((MyApplication) getApplication()).getDaoSession();
        qunbeanDao = daoSession.getQunbeanDao();
        List<Qunbean> qunbeans = qunbeanDao.loadAll();
        if (qunbeans.size() < 1) {
            return null;
        } else {
            for (Qunbean qunbean : qunbeans) {
                QunData qunData = new QunData();
                qunData.qunId = qunbean.getId();
                qunData.qunName = qunbean.getQun_name();
                qunDatas.add(qunData);
            }
        }
        return qunDatas;
    }

    public ArrayAdapter setAdapter(List list) {
        final ArrayAdapter<String> yinyongzAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, list);
        return yinyongzAdapter;
    }

    private class QunData {
        Long qunId;
        String qunName;
    }

}
